package com.facebook.imagepipeline.producers;

import android.util.Pair;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.Sets;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.imagepipeline.common.Priority;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public abstract class MultiplexProducer<K, T extends Closeable>
  implements Producer<T>
{
  private final Producer<T> mInputProducer;

  @VisibleForTesting
  @GuardedBy("this")
  final Map<K, MultiplexProducer<K, T>.Multiplexer> mMultiplexers;

  protected MultiplexProducer(Producer<T> paramProducer)
  {
    this.mInputProducer = paramProducer;
    this.mMultiplexers = new HashMap();
  }

  private MultiplexProducer<K, T>.Multiplexer createAndPutNewMultiplexer(K paramK)
  {
    try
    {
      Multiplexer localMultiplexer = new Multiplexer(paramK);
      this.mMultiplexers.put(paramK, localMultiplexer);
      return localMultiplexer;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private MultiplexProducer<K, T>.Multiplexer getExistingMultiplexer(K paramK)
  {
    try
    {
      Multiplexer localMultiplexer = (Multiplexer)this.mMultiplexers.get(paramK);
      return localMultiplexer;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private void removeMultiplexer(K paramK, MultiplexProducer<K, T>.Multiplexer paramMultiplexProducer)
  {
    try
    {
      if (this.mMultiplexers.get(paramK) == paramMultiplexProducer)
        this.mMultiplexers.remove(paramK);
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  protected abstract T cloneOrNull(T paramT);

  protected abstract K getKey(ProducerContext paramProducerContext);

  public void produceResults(Consumer<T> paramConsumer, ProducerContext paramProducerContext)
  {
    Object localObject1 = getKey(paramProducerContext);
    while (true)
      try
      {
        Multiplexer localMultiplexer = getExistingMultiplexer(localObject1);
        int i = 0;
        if (localMultiplexer == null)
        {
          localMultiplexer = createAndPutNewMultiplexer(localObject1);
          i = 1;
        }
        if (!localMultiplexer.addNewConsumer(paramConsumer, paramProducerContext))
          continue;
        if (i != 0)
          localMultiplexer.startInputProducerIfHasAttachedConsumers();
        return;
      }
      finally
      {
      }
  }

  @VisibleForTesting
  class Multiplexer
  {
    private final CopyOnWriteArraySet<Pair<Consumer<T>, ProducerContext>> mConsumerContextPairs = Sets.newCopyOnWriteArraySet();

    @Nullable
    @GuardedBy("Multiplexer.this")
    private MultiplexProducer<K, T>.Multiplexer.ForwardingConsumer mForwardingConsumer;
    private final K mKey;

    @Nullable
    @GuardedBy("Multiplexer.this")
    private T mLastIntermediateResult;

    @GuardedBy("Multiplexer.this")
    private float mLastProgress;

    @Nullable
    @GuardedBy("Multiplexer.this")
    private BaseProducerContext mMultiplexProducerContext;

    public Multiplexer()
    {
      Object localObject;
      this.mKey = localObject;
    }

    private void addCallbacks(final Pair<Consumer<T>, ProducerContext> paramPair, ProducerContext paramProducerContext)
    {
      paramProducerContext.addCallbacks(new BaseProducerContextCallbacks()
      {
        public void onCancellationRequested()
        {
          synchronized (MultiplexProducer.Multiplexer.this)
          {
            boolean bool = MultiplexProducer.Multiplexer.this.mConsumerContextPairs.remove(paramPair);
            BaseProducerContext localBaseProducerContext = null;
            List localList1 = null;
            List localList2 = null;
            List localList3 = null;
            if (bool)
            {
              if (MultiplexProducer.Multiplexer.this.mConsumerContextPairs.isEmpty())
                localBaseProducerContext = MultiplexProducer.Multiplexer.this.mMultiplexProducerContext;
            }
            else
            {
              BaseProducerContext.callOnIsPrefetchChanged(localList2);
              BaseProducerContext.callOnPriorityChanged(localList3);
              BaseProducerContext.callOnIsIntermediateResultExpectedChanged(localList1);
              if (localBaseProducerContext != null)
                localBaseProducerContext.cancel();
              if (bool)
                ((Consumer)paramPair.first).onCancellation();
              return;
            }
            localList2 = MultiplexProducer.Multiplexer.this.updateIsPrefetch();
            localList3 = MultiplexProducer.Multiplexer.this.updatePriority();
            localList1 = MultiplexProducer.Multiplexer.this.updateIsIntermediateResultExpected();
            localBaseProducerContext = null;
          }
        }

        public void onIsIntermediateResultExpectedChanged()
        {
          BaseProducerContext.callOnIsIntermediateResultExpectedChanged(MultiplexProducer.Multiplexer.this.updateIsIntermediateResultExpected());
        }

        public void onIsPrefetchChanged()
        {
          BaseProducerContext.callOnIsPrefetchChanged(MultiplexProducer.Multiplexer.this.updateIsPrefetch());
        }

        public void onPriorityChanged()
        {
          BaseProducerContext.callOnPriorityChanged(MultiplexProducer.Multiplexer.this.updatePriority());
        }
      });
    }

    private void closeSafely(Closeable paramCloseable)
    {
      if (paramCloseable != null);
      try
      {
        paramCloseable.close();
        return;
      }
      catch (IOException localIOException)
      {
        throw new RuntimeException(localIOException);
      }
    }

    private boolean computeIsIntermediateResultExpected()
    {
      try
      {
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        boolean bool2;
        do
        {
          if (!localIterator.hasNext())
            break;
          bool2 = ((ProducerContext)((Pair)localIterator.next()).second).isIntermediateResultExpected();
        }
        while (!bool2);
        for (boolean bool1 = true; ; bool1 = false)
          return bool1;
      }
      finally
      {
      }
    }

    private boolean computeIsPrefetch()
    {
      try
      {
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        boolean bool2;
        do
        {
          if (!localIterator.hasNext())
            break;
          bool2 = ((ProducerContext)((Pair)localIterator.next()).second).isPrefetch();
        }
        while (bool2);
        for (boolean bool1 = false; ; bool1 = true)
          return bool1;
      }
      finally
      {
      }
    }

    private Priority computePriority()
    {
      try
      {
        Object localObject2 = Priority.LOW;
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        while (localIterator.hasNext())
        {
          Priority localPriority = Priority.getHigherPriority((Priority)localObject2, ((ProducerContext)((Pair)localIterator.next()).second).getPriority());
          localObject2 = localPriority;
        }
        return localObject2;
      }
      finally
      {
      }
    }

    private void startInputProducerIfHasAttachedConsumers()
    {
      boolean bool1 = true;
      while (true)
      {
        try
        {
          if (this.mMultiplexProducerContext == null)
          {
            bool2 = bool1;
            Preconditions.checkArgument(bool2);
            if (this.mForwardingConsumer != null)
              break label188;
            Preconditions.checkArgument(bool1);
            if (this.mConsumerContextPairs.isEmpty())
            {
              MultiplexProducer.this.removeMultiplexer(this.mKey, this);
              return;
            }
            ProducerContext localProducerContext = (ProducerContext)((Pair)this.mConsumerContextPairs.iterator().next()).second;
            this.mMultiplexProducerContext = new BaseProducerContext(localProducerContext.getImageRequest(), localProducerContext.getId(), localProducerContext.getListener(), localProducerContext.getCallerContext(), localProducerContext.getLowestPermittedRequestLevel(), computeIsPrefetch(), computeIsIntermediateResultExpected(), computePriority());
            this.mForwardingConsumer = new ForwardingConsumer(null);
            BaseProducerContext localBaseProducerContext = this.mMultiplexProducerContext;
            ForwardingConsumer localForwardingConsumer = this.mForwardingConsumer;
            MultiplexProducer.this.mInputProducer.produceResults(localForwardingConsumer, localBaseProducerContext);
            return;
          }
        }
        finally
        {
        }
        boolean bool2 = false;
        continue;
        label188: bool1 = false;
      }
    }

    @Nullable
    private List<ProducerContextCallbacks> updateIsIntermediateResultExpected()
    {
      try
      {
        BaseProducerContext localBaseProducerContext = this.mMultiplexProducerContext;
        if (localBaseProducerContext == null);
        List localList;
        for (Object localObject2 = null; ; localObject2 = localList)
        {
          return localObject2;
          localList = this.mMultiplexProducerContext.setIsIntermediateResultExpectedNoCallbacks(computeIsIntermediateResultExpected());
        }
      }
      finally
      {
      }
    }

    @Nullable
    private List<ProducerContextCallbacks> updateIsPrefetch()
    {
      try
      {
        BaseProducerContext localBaseProducerContext = this.mMultiplexProducerContext;
        if (localBaseProducerContext == null);
        List localList;
        for (Object localObject2 = null; ; localObject2 = localList)
        {
          return localObject2;
          localList = this.mMultiplexProducerContext.setIsPrefetchNoCallbacks(computeIsPrefetch());
        }
      }
      finally
      {
      }
    }

    @Nullable
    private List<ProducerContextCallbacks> updatePriority()
    {
      try
      {
        BaseProducerContext localBaseProducerContext = this.mMultiplexProducerContext;
        if (localBaseProducerContext == null);
        List localList;
        for (Object localObject2 = null; ; localObject2 = localList)
        {
          return localObject2;
          localList = this.mMultiplexProducerContext.setPriorityNoCallbacks(computePriority());
        }
      }
      finally
      {
      }
    }

    // ERROR //
    public boolean addNewConsumer(Consumer<T> paramConsumer, ProducerContext paramProducerContext)
    {
      // Byte code:
      //   0: aload_1
      //   1: aload_2
      //   2: invokestatic 218	android/util/Pair:create	(Ljava/lang/Object;Ljava/lang/Object;)Landroid/util/Pair;
      //   5: astore_3
      //   6: aload_0
      //   7: monitorenter
      //   8: aload_0
      //   9: getfield 31	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:this$0	Lcom/facebook/imagepipeline/producers/MultiplexProducer;
      //   12: aload_0
      //   13: getfield 44	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:mKey	Ljava/lang/Object;
      //   16: invokestatic 222	com/facebook/imagepipeline/producers/MultiplexProducer:access$100	(Lcom/facebook/imagepipeline/producers/MultiplexProducer;Ljava/lang/Object;)Lcom/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer;
      //   19: aload_0
      //   20: if_acmpeq +7 -> 27
      //   23: aload_0
      //   24: monitorexit
      //   25: iconst_0
      //   26: ireturn
      //   27: aload_0
      //   28: getfield 42	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:mConsumerContextPairs	Ljava/util/concurrent/CopyOnWriteArraySet;
      //   31: aload_3
      //   32: invokevirtual 226	java/util/concurrent/CopyOnWriteArraySet:add	(Ljava/lang/Object;)Z
      //   35: pop
      //   36: aload_0
      //   37: invokespecial 61	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:updateIsPrefetch	()Ljava/util/List;
      //   40: astore 6
      //   42: aload_0
      //   43: invokespecial 65	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:updatePriority	()Ljava/util/List;
      //   46: astore 7
      //   48: aload_0
      //   49: invokespecial 69	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:updateIsIntermediateResultExpected	()Ljava/util/List;
      //   52: astore 8
      //   54: aload_0
      //   55: getfield 228	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:mLastIntermediateResult	Ljava/io/Closeable;
      //   58: astore 9
      //   60: aload_0
      //   61: getfield 230	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:mLastProgress	F
      //   64: fstore 10
      //   66: aload_0
      //   67: monitorexit
      //   68: aload 6
      //   70: invokestatic 234	com/facebook/imagepipeline/producers/BaseProducerContext:callOnIsPrefetchChanged	(Ljava/util/List;)V
      //   73: aload 7
      //   75: invokestatic 237	com/facebook/imagepipeline/producers/BaseProducerContext:callOnPriorityChanged	(Ljava/util/List;)V
      //   78: aload 8
      //   80: invokestatic 240	com/facebook/imagepipeline/producers/BaseProducerContext:callOnIsIntermediateResultExpectedChanged	(Ljava/util/List;)V
      //   83: aload_3
      //   84: monitorenter
      //   85: aload_0
      //   86: monitorenter
      //   87: aload 9
      //   89: aload_0
      //   90: getfield 228	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:mLastIntermediateResult	Ljava/io/Closeable;
      //   93: if_acmpeq +60 -> 153
      //   96: aconst_null
      //   97: astore 9
      //   99: aload_0
      //   100: monitorexit
      //   101: aload 9
      //   103: ifnull +33 -> 136
      //   106: fload 10
      //   108: fconst_0
      //   109: fcmpl
      //   110: ifle +11 -> 121
      //   113: aload_1
      //   114: fload 10
      //   116: invokeinterface 246 2 0
      //   121: aload_1
      //   122: aload 9
      //   124: iconst_0
      //   125: invokeinterface 250 3 0
      //   130: aload_0
      //   131: aload 9
      //   133: invokespecial 252	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:closeSafely	(Ljava/io/Closeable;)V
      //   136: aload_3
      //   137: monitorexit
      //   138: aload_0
      //   139: aload_3
      //   140: aload_2
      //   141: invokespecial 254	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:addCallbacks	(Landroid/util/Pair;Lcom/facebook/imagepipeline/producers/ProducerContext;)V
      //   144: iconst_1
      //   145: ireturn
      //   146: astore 4
      //   148: aload_0
      //   149: monitorexit
      //   150: aload 4
      //   152: athrow
      //   153: aload 9
      //   155: ifnull -56 -> 99
      //   158: aload_0
      //   159: getfield 31	com/facebook/imagepipeline/producers/MultiplexProducer$Multiplexer:this$0	Lcom/facebook/imagepipeline/producers/MultiplexProducer;
      //   162: aload 9
      //   164: invokevirtual 258	com/facebook/imagepipeline/producers/MultiplexProducer:cloneOrNull	(Ljava/io/Closeable;)Ljava/io/Closeable;
      //   167: astore 9
      //   169: goto -70 -> 99
      //   172: astore 12
      //   174: aload_0
      //   175: monitorexit
      //   176: aload 12
      //   178: athrow
      //   179: astore 11
      //   181: aload_3
      //   182: monitorexit
      //   183: aload 11
      //   185: athrow
      //
      // Exception table:
      //   from	to	target	type
      //   8	25	146	finally
      //   27	68	146	finally
      //   148	150	146	finally
      //   87	96	172	finally
      //   99	101	172	finally
      //   158	169	172	finally
      //   174	176	172	finally
      //   85	87	179	finally
      //   113	121	179	finally
      //   121	136	179	finally
      //   136	138	179	finally
      //   176	179	179	finally
      //   181	183	179	finally
    }

    public void onCancelled(MultiplexProducer<K, T>.Multiplexer.ForwardingConsumer paramMultiplexProducer)
    {
      try
      {
        if (this.mForwardingConsumer != paramMultiplexProducer)
          return;
        this.mForwardingConsumer = null;
        this.mMultiplexProducerContext = null;
        closeSafely(this.mLastIntermediateResult);
        this.mLastIntermediateResult = null;
        startInputProducerIfHasAttachedConsumers();
        return;
      }
      finally
      {
      }
    }

    public void onFailure(MultiplexProducer<K, T>.Multiplexer.ForwardingConsumer paramMultiplexProducer, Throwable paramThrowable)
    {
      try
      {
        if (this.mForwardingConsumer != paramMultiplexProducer)
          return;
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        this.mConsumerContextPairs.clear();
        MultiplexProducer.this.removeMultiplexer(this.mKey, this);
        closeSafely(this.mLastIntermediateResult);
        this.mLastIntermediateResult = null;
        while (localIterator.hasNext())
          synchronized ((Pair)localIterator.next())
          {
            ((Consumer)???.first).onFailure(paramThrowable);
          }
      }
      finally
      {
      }
    }

    public void onNextResult(MultiplexProducer<K, T>.Multiplexer.ForwardingConsumer paramMultiplexProducer, T paramT, boolean paramBoolean)
    {
      try
      {
        if (this.mForwardingConsumer != paramMultiplexProducer)
          return;
        closeSafely(this.mLastIntermediateResult);
        this.mLastIntermediateResult = null;
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        if (!paramBoolean)
          this.mLastIntermediateResult = MultiplexProducer.this.cloneOrNull(paramT);
        while (true)
        {
          while (localIterator.hasNext())
            synchronized ((Pair)localIterator.next())
            {
              ((Consumer)???.first).onNewResult(paramT, paramBoolean);
            }
          this.mConsumerContextPairs.clear();
          MultiplexProducer.this.removeMultiplexer(this.mKey, this);
        }
      }
      finally
      {
      }
    }

    public void onProgressUpdate(MultiplexProducer<K, T>.Multiplexer.ForwardingConsumer paramMultiplexProducer, float paramFloat)
    {
      try
      {
        if (this.mForwardingConsumer != paramMultiplexProducer)
          return;
        this.mLastProgress = paramFloat;
        Iterator localIterator = this.mConsumerContextPairs.iterator();
        while (localIterator.hasNext())
          synchronized ((Pair)localIterator.next())
          {
            ((Consumer)???.first).onProgressUpdate(paramFloat);
          }
      }
      finally
      {
      }
    }

    private class ForwardingConsumer extends BaseConsumer<T>
    {
      private ForwardingConsumer()
      {
      }

      protected void onCancellationImpl()
      {
        MultiplexProducer.Multiplexer.this.onCancelled(this);
      }

      protected void onFailureImpl(Throwable paramThrowable)
      {
        MultiplexProducer.Multiplexer.this.onFailure(this, paramThrowable);
      }

      protected void onNewResultImpl(T paramT, boolean paramBoolean)
      {
        MultiplexProducer.Multiplexer.this.onNextResult(this, paramT, paramBoolean);
      }

      protected void onProgressUpdateImpl(float paramFloat)
      {
        MultiplexProducer.Multiplexer.this.onProgressUpdate(this, paramFloat);
      }
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.producers.MultiplexProducer
 * JD-Core Version:    0.6.2
 */